/**
 * 
 */
package it.nola.soccer.listener;

import it.nola.soccer.DbAdapter;
import it.nola.soccer.DbConst;
import it.nola.soccer.SubstEdit;
import android.database.Cursor;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;

/**
 * @author paolo
 */
public class TeamSubstItemListener implements OnItemSelectedListener {
    Spinner player_inSpinner;
    Spinner player_outSpinner;
    DbAdapter dba;
    long cronaca;
    SubstEdit substEdit;

    public TeamSubstItemListener(SubstEdit se) {
	substEdit=se;
	player_inSpinner=se.getPlayer_inSpinner();
	player_outSpinner=se.getPlayer_outSpinner();
	dba=se.getDbHelper();
	cronaca=se.getCronaca();
    }

    /**
     * Abilita i campi player in e player out e riempie con i valori di binding
     * 
     * @see android.widget.AdapterView.OnItemSelectedListener#onItemSelected(android.widget.AdapterView,
     *      android.view.View, int, long)
     */
    public void onItemSelected(AdapterView<?> parent, View v, int pos, long row) {
	player_inSpinner.setEnabled(true);
	player_outSpinner.setEnabled(true);
	Cursor playerCur=dba.getAllPlayers(cronaca, pos+1); // pos 0 => sqaudra 1; pos =1 =>
							    // squadra=2
	SimpleCursorAdapter playerAdapter=new SimpleCursorAdapter(substEdit, android.R.layout.simple_spinner_item,
	    playerCur, new String[]{DbConst.players.KEY_NAME }, new int[]{android.R.id.text1 });
	playerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	player_inSpinner.setAdapter(playerAdapter);
	player_outSpinner.setAdapter(playerAdapter);
    }

    /**
     * Disabilita i campi player in e player out e cancella i valori di binding
     * 
     * @see android.widget.AdapterView.OnItemSelectedListener#onNothingSelected(android.widget.
     *      AdapterView )
     */
    public void onNothingSelected(AdapterView<?> arg0) {
	player_inSpinner.setEnabled(false);
	player_outSpinner.setEnabled(false);
    }

}
